package cn.jboa.service.impl;

import cn.jboa.common.Constants;
import cn.jboa.entity.SysEmployee;
import org.springframework.stereotype.Service;

import java.util.*;
@Service("claimVoucherServiceFM")
public class ClaimVoucherServiceImplFM extends BaseClaimVoucherServiceImpl{
    private static Map<String, String> statusMap;
    static {
        statusMap = new LinkedHashMap<>();
        statusMap.put(Constants.CLAIMVOUCHER_SUBMITTED, "已提交");
        statusMap.put(Constants.CLAIMVOUCHER_APPROVING, "待审批");
        statusMap.put(Constants.CLAIMVOUCHER_BACK, "已打回");
        statusMap.put(Constants.CLAIMVOUCHER_APPROVED, "已审批");
        statusMap.put(Constants.CLAIMVOUCHER_PAID, "已付款");
        statusMap.put(Constants.CLAIMVOUCHER_TERMINATED, "已终止");
    }
    @Override
    protected void buildBaseHql(SysEmployee emp, String status, Date startDate, Date endDate, StringBuilder hqlBuilder, List<Object> values) {

        hqlBuilder
                .append("select v from BizClaimVoucher v, SysEmployee e where v.creator.sn = e.sn and e.sysDepartment.id = ?");
        values.add(emp.getSysDepartment().getId());
        if (status != null && status.length() != 0) {
            hqlBuilder.append(" and v.status = ?");
            values.add(status);
        } else {
            hqlBuilder.append(" and v.status >= ?");
            values.add(Constants.CLAIMVOUCHER_SUBMITTED);
        }
        if (startDate != null) {
            hqlBuilder.append(" and v.createTime >= ?");// yyyy-MM-dd 00:00:00
            values.add(startDate);
        }
        if (endDate != null) {
            hqlBuilder.append(" and v.createTime < ?");// yyyy-MM-dd 00:00:00
            Calendar oneDayLater = Calendar.getInstance();
            oneDayLater.setTime(endDate);
            oneDayLater.add(Calendar.DAY_OF_MONTH, 1);
            values.add(oneDayLater.getTime());
        }

    }

    @Override
    protected void addOrders(String status, StringBuilder hqlBuilder) {
        if (status == null || status.length() == 0)
            hqlBuilder.append(" order by v.status, v.createTime desc");
        else
            hqlBuilder.append(" order by v.createTime desc");
    }
    @Override
    public Map<String, String> getStatusMap() {
        return statusMap;
    }
}
